package com.unience.ec2.eclipse.view;

import java.net.URL;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;

import com.unience.ec2.eclipse.config.Config;
import com.xerox.amazonws.sqs2.MessageQueue;
import com.xerox.amazonws.sqs2.QueueService;
import com.xerox.amazonws.sqs2.SQSException;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class SQSViewPart extends ViewPart {
	protected final Log log = LogFactory.getLog(this.getClass().getName());
	private Label labelNumMsg;
	private ProgressBar progressBarQueue;
	private Label labelNameQueue;

	public static final String ID_VIEW = "com.unience.ec2.eclipse.view.SQSViewPart"; //$NON-NLS-1$

	Composite composite1;
	private Composite compositeQueueInfo;
	private Composite compositeQueues;

	SQSMonitor sqsmonitor;
	SQSViewPart monitor = null;
	
	
	private HashMap<String, QueueUI> queueInfo;

	
	String queueName;
	int numMessages;
	/**
	 * 
	 */
	public SQSViewPart() {
		super();
		queueInfo = new HashMap<String, QueueUI>();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IWorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	public void createPartControl(Composite parent) {
		{
			parent.setSize(836, 700);
		}
		GridLayout composite1Layout = new GridLayout();
		{

			composite1 = new Composite(parent, SWT.NONE);
			GridLayout composite1Layout1 = new GridLayout();

			GridData composite1LData = new GridData();
			composite1LData.widthHint = 638;
			composite1LData.heightHint = 35;
			composite1.setLayoutData(composite1LData);
			composite1.setLayout(composite1Layout1);

			{
				compositeQueues = new Composite(composite1, SWT.NONE);
				GridLayout compositeQueuesLayout = new GridLayout();
				composite1Layout1.numColumns = 1;
				compositeQueuesLayout.makeColumnsEqualWidth = true;
				GridData compositeQueuesLData = new GridData();
				compositeQueuesLData.widthHint = 830;
				compositeQueuesLData.heightHint = 426;
				compositeQueues.setLayoutData(compositeQueuesLData);
				compositeQueues.setLayout(compositeQueuesLayout);

			}
			{
				compositeQueueInfo = new Composite(composite1, SWT.NONE);
				GridLayout compositeQueueInfoLayout = new GridLayout();
				compositeQueueInfoLayout.makeColumnsEqualWidth = true;
				GridData compositeQueueInfoLData = new GridData();
				compositeQueueInfoLData.widthHint = 831;
				compositeQueueInfoLData.heightHint = 389;
				compositeQueueInfo.setLayoutData(compositeQueueInfoLData);
				compositeQueueInfo.setLayout(compositeQueueInfoLayout);
			}

		}

		load();
	}

	private void newQueueinfo(String queueName, int numMessages) {
		QueueUI queueUI = new QueueUI(queueName, numMessages,compositeQueues);
		compositeQueues.layout();
		queueInfo.put(queueName,queueUI);
		
	}

	private void load() {
		Config config = Config.getInstance();

		sqsmonitor = new SQSMonitor(config.getKeyId(), config.getSecretId(),
				this);

		sqsmonitor.start();

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IWorkbenchPart#setFocus()
	 */
	public void setFocus() {
		// TODO Auto-generated method stub
	}

	/**
	 * Cleans up all resources created by this ViewPart.
	 */
	public void dispose() {
		super.dispose();
	}

	private void showQueues(List<MessageQueue> queues) {
		
		for (MessageQueue messageQueue : queues) {
			try {
				int numMessages = messageQueue.getApproximateNumberOfMessages();
				
				URL url = messageQueue.getUrl();
				
				String queueName = url.getPath();
				
				QueueUI queueUI = queueInfo.get(queueName);
				
				if ( queueUI != null){
					queueUI.update(numMessages);
					
				}else{
					this.newQueueinfo(queueName, numMessages);
				}
				
				
			} catch (SQSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}

	}

	public void update(final List<MessageQueue> queues) {
		monitor = this;

		for (MessageQueue messageQueue : queues) {
			try {
				numMessages = messageQueue.getApproximateNumberOfMessages();
				
				URL url = messageQueue.getUrl();
				
				queueName = url.getPath();
				
				Config.getInstance().getDisplay().asyncExec(new Runnable() {
					public void run() {

						final IWorkbench workbench = PlatformUI.getWorkbench();
						if (workbench == null)
							return;
						final Display display = workbench.getDisplay();

						if (!display.isDisposed()) {
							log.info("updating Queues numInstances " + queues.size());
							monitor.showQueues(queues);
							QueueUI queueUI = queueInfo.get(queueName);
							
							if ( queueUI != null){
								queueUI.update(numMessages);
								
							}else{
								monitor.newQueueinfo(queueName, numMessages);
							}
						}
					}
				});
				
				
			} catch (SQSException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		

	}

	class SQSMonitor extends Thread {

		private QueueService qs;
		private SQSViewPart gui;

		private boolean stop = false;

		public SQSMonitor(String keyId, String secretId, SQSViewPart gui) {

			this.gui = gui;
			qs = new QueueService(keyId, secretId);

		}

		public void setStop(boolean stop) {
			this.stop = stop;
		}

		public void run() {

			while (!stop) {
				try {
					List<MessageQueue> listqueues = qs.listMessageQueues(null);
					gui.update(listqueues);
				} catch (SQSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				try {
					sleep(60*1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}

		}
	}

	
	class QueueUI{
		ProgressBar progressBarQueue;
		Label labelNumMsg;
		Label labelNameQueue;
		Composite compositeNewQueues;
		
		public QueueUI(String queueName, int numMessages, Composite compositeQueues) {
			
			this.compositeNewQueues = new Composite(compositeQueues, SWT.NONE);
			GridLayout compositeQueuesLayout = new GridLayout();
			compositeQueuesLayout.numColumns = 3;
			compositeQueuesLayout.makeColumnsEqualWidth = false;
			this.compositeNewQueues.setLayout(compositeQueuesLayout);
			this.compositeNewQueues.setSize(413, 50);

			
			{
				GridData progressBarQueueLData = new GridData();
				this.progressBarQueue = new ProgressBar(compositeNewQueues, SWT.NONE);
				this.progressBarQueue.setLayoutData(progressBarQueueLData);
				
				
				this.progressBarQueue.setMinimum(0);
				this.progressBarQueue.setMaximum(100);
				
				this.progressBarQueue.setSelection(numMessages);
			}

			{
				this.labelNameQueue = new Label(compositeNewQueues, SWT.NONE);
				GridData labelNameQueueLData = new GridData();
				labelNameQueueLData.widthHint = 124;
				labelNameQueueLData.heightHint = 17;
				this.labelNameQueue.setLayoutData(labelNameQueueLData);
				this.labelNameQueue.setText(queueName);
			}
			{
				this.labelNumMsg = new Label(compositeNewQueues, SWT.NONE);
				GridData labelNumMsgLData = new GridData();
				this.labelNumMsg.setLayoutData(labelNumMsgLData);
				this.labelNumMsg.setText(Integer.toString(numMessages));
			}
		}
		
		public void update(int numMessages){
			this.progressBarQueue.setSelection(numMessages);
			this.progressBarQueue.setToolTipText(Integer.toString(numMessages));
			this.labelNumMsg.setText(Integer.toString(numMessages));
			compositeNewQueues.layout();
		}
	}
}
